package com.agentlab.imitation.web.action.admin;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.agentlab.imitation.bean.admin.Admin;
import com.agentlab.imitation.common.CommonConstant;
import com.agentlab.imitation.service.admin.AdminService;
import com.agentlab.imitation.util.DateTimeUtils;
import com.agentlab.imitation.util.MD5;

/**
 * @category 修改密码
 * 	
 */
@Controller
public class UpdateAdminPassActon  {
	@Resource
	AdminService usersService;

	/**
	 * 进入用户修改密码界面
	 */
//	@Permission(module="sysuser", privilege="psupdate")
	public String  userPassUI(){
		return "userpass";
	}
	
	/**
	 * 用户修改密码
	 */
//	@Permission(module="sysuser", privilege="psupdate")
	public String userPass(){
		HttpServletRequest req = ServletActionContext.getRequest();
		Admin user =(Admin)req.getSession().getAttribute(CommonConstant.ADMIN_SESSION);
		if(user == null){
			return CommonConstant.ADMIN_NO_PERMISSION;
		}
		
		String newuserpass = req.getParameter("newuserpass");
		//取得新密码
		if(newuserpass == null || newuserpass.equals("")){ 
			req.setAttribute("message", "修改失败！新密码不能为空");
			return "userpass" ;
		}
		//判断原密码是否输入正确
		String olduserpass = req.getParameter("olduserpass");
		if(!MD5.MD5Encode(olduserpass).equals(user.getPassword())){
			req.setAttribute("message", "修改失败！当前密码错误");
			return "userpass" ;
		}
		
		user.setPassword(MD5.MD5Encode(newuserpass));
//		user.setUpdatetime(DateTimeUtils.getDateTimeStamp());
		usersService.update(user);
		req.getSession().setAttribute(CommonConstant.ADMIN_SESSION, user);//将更新后的user写回session
		req.setAttribute("message", "修改成功");
		return "userpass" ;
		
	}
}
